home *** CD-ROM | disk | FTP | other *** search
- Public Class EventLogForm
- Inherits System.Windows.Forms.Form
-
- #Region " Windows Form Designer generated code "
-
- Public Sub New()
- MyBase.New()
-
- 'This call is required by the Windows Form Designer.
- InitializeComponent()
-
- 'Add any initialization after the InitializeComponent() call
-
- End Sub
-
- 'Form overrides dispose to clean up the component list.
- Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
- If disposing Then
- If Not (components Is Nothing) Then
- components.Dispose()
- End If
- End If
- MyBase.Dispose(disposing)
- End Sub
- Friend WithEvents btnListLogs As System.Windows.Forms.Button
- Friend WithEvents txtOut As System.Windows.Forms.TextBox
- Friend WithEvents btnListApplication As System.Windows.Forms.Button
- Friend WithEvents btnSQLEntries As System.Windows.Forms.Button
- Friend WithEvents btnSQLEvents As System.Windows.Forms.Button
- Friend WithEvents btnWrite As System.Windows.Forms.Button
-
- 'Required by the Windows Form Designer
- Private components As System.ComponentModel.Container
-
- 'NOTE: The following procedure is required by the Windows Form Designer
- 'It can be modified using the Windows Form Designer.
- 'Do not modify it using the code editor.
- <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
- Me.txtOut = New System.Windows.Forms.TextBox()
- Me.btnListLogs = New System.Windows.Forms.Button()
- Me.btnSQLEvents = New System.Windows.Forms.Button()
- Me.btnListApplication = New System.Windows.Forms.Button()
- Me.btnSQLEntries = New System.Windows.Forms.Button()
- Me.btnWrite = New System.Windows.Forms.Button()
- Me.SuspendLayout()
- '
- 'txtOut
- '
- Me.txtOut.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
- Or System.Windows.Forms.AnchorStyles.Left) _
- Or System.Windows.Forms.AnchorStyles.Right)
- Me.txtOut.Location = New System.Drawing.Point(160, 16)
- Me.txtOut.Multiline = True
- Me.txtOut.Name = "txtOut"
- Me.txtOut.ScrollBars = System.Windows.Forms.ScrollBars.Both
- Me.txtOut.Size = New System.Drawing.Size(344, 288)
- Me.txtOut.TabIndex = 1
- Me.txtOut.Text = ""
- Me.txtOut.WordWrap = False
- '
- 'btnListLogs
- '
- Me.btnListLogs.Location = New System.Drawing.Point(16, 16)
- Me.btnListLogs.Name = "btnListLogs"
- Me.btnListLogs.Size = New System.Drawing.Size(120, 40)
- Me.btnListLogs.TabIndex = 0
- Me.btnListLogs.Text = "List Event logs"
- '
- 'btnSQLEvents
- '
- Me.btnSQLEvents.Location = New System.Drawing.Point(16, 160)
- Me.btnSQLEvents.Name = "btnSQLEvents"
- Me.btnSQLEvents.Size = New System.Drawing.Size(120, 40)
- Me.btnSQLEvents.TabIndex = 0
- Me.btnSQLEvents.Text = "Get MSSQL events"
- '
- 'btnListApplication
- '
- Me.btnListApplication.Location = New System.Drawing.Point(16, 64)
- Me.btnListApplication.Name = "btnListApplication"
- Me.btnListApplication.Size = New System.Drawing.Size(120, 40)
- Me.btnListApplication.TabIndex = 0
- Me.btnListApplication.Text = "List Application entries"
- '
- 'btnSQLEntries
- '
- Me.btnSQLEntries.Location = New System.Drawing.Point(16, 112)
- Me.btnSQLEntries.Name = "btnSQLEntries"
- Me.btnSQLEntries.Size = New System.Drawing.Size(120, 40)
- Me.btnSQLEntries.TabIndex = 0
- Me.btnSQLEntries.Text = "List SQL Server entries"
- '
- 'btnWrite
- '
- Me.btnWrite.Location = New System.Drawing.Point(16, 208)
- Me.btnWrite.Name = "btnWrite"
- Me.btnWrite.Size = New System.Drawing.Size(120, 40)
- Me.btnWrite.TabIndex = 0
- Me.btnWrite.Text = "Write event logs"
- '
- 'EventLogForm
- '
- Me.AutoScaleBaseSize = New System.Drawing.Size(7, 17)
- Me.ClientSize = New System.Drawing.Size(520, 317)
- Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnWrite, Me.btnSQLEvents, Me.btnSQLEntries, Me.btnListApplication, Me.txtOut, Me.btnListLogs})
- Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 11!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
- Me.Name = "EventLogForm"
- Me.Text = "Event Logging"
- Me.ResumeLayout(False)
-
- End Sub
-
- #End Region
-
- ' a routine that shows output in the textbox control
-
- Sub LogMessage(ByVal msg As String)
- txtOut.AppendText(msg & ControlChars.CrLf)
- End Sub
-
- ' List existing logs.
-
- Private Sub btnListLogs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnListLogs.Click
- txtOut.Text = ""
- Dim evlog As EventLog
- For Each evlog In EventLog.GetEventLogs
- LogMessage(evlog.Log)
- Next
- End Sub
-
- ' list the Application event log
-
- Private Sub btnListApplication_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnListApplication.Click
- txtOut.Text = ""
-
- ' Get a reference to the Application eventlog.
- Dim elApp As New EventLog("Application")
- Dim entry As EventLogEntry
-
- ' NOTE: this loop usually generates too much info to be displayed in the textbox
- ' so expect that the output be truncated at some point.
- For Each entry In elApp.Entries
- LogMessage(GetEntryInfo(entry))
- Next
- End Sub
-
- ' Return readable information on an event log entry.
-
- Private Function GetEntryInfo(ByVal entry As EventLogEntry) As String
- ' This schema is similar to the info you see in Event Viewer.
- Dim sb As New System.Text.StringBuilder(200)
- sb.Append(entry.EntryType.ToString)
- sb.Append(" ")
- sb.Append(entry.TimeGenerated)
- sb.Append(" ")
- sb.Append(entry.Source)
- sb.Append(" ")
- sb.Append(entry.Category)
- sb.Append(" ")
- sb.Append(entry.EventID)
- sb.Append(" ")
- sb.Append(entry.UserName)
- sb.Append(" ")
- sb.Append(entry.Message)
- sb.Append(" ")
-
- Return sb.ToString
-
- End Function
-
- ' display log messages from SQL Server
-
- Private Sub btnSQLEntries_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSQLEntries.Click
- txtOut.Text = ""
-
- ' Get a reference to the Application eventlog.
- Dim elApp As New EventLog("Application", ".")
- Dim entry As EventLogEntry
-
- For Each entry In elApp.Entries
- If entry.Source = "MSSQLServer" Then
- LogMessage(GetEntryInfo(entry))
- End If
- Next
- End Sub
-
- ' enable trapping events from SQL Server
-
- Private Sub btnSQLEvents_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSQLEvents.Click
- ' We're interested in SQL Server events on local machine only.
- Dim sourceName As String = "MSSQLServer"
-
- ' Exit if the source isn't registered.
- If Not EventLog.SourceExists(sourceName) Then Exit Sub
- ' Get the name of the log that contains entries from SQL Server.
- Dim logName As String = EventLog.LogNameFromSourceName(sourceName, ".")
- ' Get the corresponding EventLog object.
- Dim evLog As New EventLog(logName)
- ' Associate it to an event handler.
- AddHandler evLog.EntryWritten, AddressOf EntryWritten
- ' This is very important.
- evLog.EnableRaisingEvents = True
-
- txtOut.Text = ""
- End Sub
-
- ' this event fires when an entry is written to the log by SQL Server
-
- Private Sub EntryWritten(ByVal sender As Object, ByVal e As System.Diagnostics.EntryWrittenEventArgs)
- If e.Entry.Source = "MSSQLServer" Then
- LogMessage(GetEntryInfo(e.Entry))
- End If
- End Sub
-
- ' write to the event log
-
- Private Sub btnWrite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWrite.Click
- ' Register your demo app as an event source.
- EventLog.CreateEventSource("DemoApp2", "XXX")
- ' Create an EventLog object connected to that event log.
- Dim evLog As New EventLog("XXX", ".", "DemoApp2")
-
- ' Write two entries to the Application log.
- evLog.WriteEntry("First message")
- System.Threading.Thread.Sleep(500)
- evLog.WriteEntry("Second message")
-
- ' Write an error message.
- evLog.WriteEntry("Third message", EventLogEntryType.Error)
- ' Write a warning error with an application-defined event id.
- evLog.WriteEntry("Fourth message", EventLogEntryType.Warning, 123)
- ' Write a warning error with an application-defined event id and category id.
- evLog.WriteEntry("Fifth message", EventLogEntryType.Warning, 123, 456)
- ' Write a warning error with an application-defined event id and category id,
- ' plus associated binary data.
- Dim bytes() As Byte = {0, 2, 4, 6, 8, 10, 12}
- evLog.WriteEntry("Fifth message", EventLogEntryType.Warning, 123, 456, bytes)
-
- ' Delete this event source.
- EventLog.DeleteEventSource("DemoApp2")
- End Sub
- End Class
-